#ifndef _MMC5983MA_H_
#define _MMC5983MA_H_

#include "Arduino.h"
#include "Wire.h"

#define MAG_ODR_1HZ       0b1001
#define MAG_ODR_10HZ      0b1010
#define MAG_ODR_20HZ      0b1011
#define MAG_ODR_50HZ      0b1100
#define MAG_ODR_100HZ     0b1101
#define MAG_ODR_200HZ     0b1110
#define MAG_ODR_1000HZ    0b1111

#define MAG_ADDR      0x30
#define MAG_RATE      MAG_ODR_200HZ

#define LSB_PER_GAUSS   4096

class MMC5983MA
{
public:
    void begin();
    void cal();
    void peeling();
    void clear_peeling();
    float get_Xraw();
    float get_Yraw();
    float get_Zraw();
    float get_heading();
    float get_field_strength();


private:
    float max_x, max_y, max_z;
    float min_x, min_y, min_z;
    float peeling_x = 0.0,peeling_y = 0.0,peeling_z = 0.0;
    int is_peeling = 0;
    float xoffs = 0.0, yoffs = 0.0, zoffs = 0.0;
    float xscale, yscale, zscale;

    float xalign[3] = {0, 0, 0};
    float yalign[3] = {0, 0, 0};
    float zalign[3] = {0, 0, 0};

    float x, y, z;
    int32_t rx, ry, rz;
    uint8_t chip=0, productId=0;
    uint8_t readReg(uint8_t addr, uint8_t reg);
    void readReg(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t size);
    void writeReg(uint8_t addr, uint8_t reg, uint8_t val);
    void readMagRaw(int32_t &x, int32_t &y, int32_t &z);
    void judgeId();
};
#endif